import { getQuestionnaireList } from '@/api/modules/flow'
import { departments } from '@/api/modules/departments'
const state = {
  QuestionnaireInfoList: [],
  DepartmentList: [],
}

const mutations = {
  QUESTIONNAIRE_LIST: (state, list) => {
    state.QuestionnaireInfoList = list
  },
  DEPARTMRNT_LIST: (state, list) => {
    state.DepartmentList = list
  },
}

const actions = {
  // TODO 设置时间缓存
  getQuestionnaireInfoList({ commit }) {
    return new Promise((resolve, reject) => {
      getQuestionnaireList({})
        .then((res) => {
          resolve(res.data)
          commit('QUESTIONNAIRE_LIST', res.data)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  getDepartmentList({ commit }) {
    console.log('state.DepartmentList = ', state.DepartmentList.length)
    if (state.DepartmentList.length > 0)
      return Promise.resolve(state.DepartmentList)
    return new Promise((resolve, reject) => {
      const search = { pageIndex: 1, pageSize: 500, status: 1 }
      departments(search)
        .then((res) => {
          const data = res.data.map((item) => {
            return { value: item.id, label: item.name }
          })
          commit('DEPARTMRNT_LIST', data)
          resolve(data)
        })
        .then((err) => {
          reject(err)
        })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
